var accountstore;
var accountGrid;
var addWinAccountGrid;
function getMenu(tree) {

	Ext.Ajax.request({
				url : 'orgChartTree!orgChartTree.action',
				method : "post",
				success : function(response, options) {
					var responseTextVale = Ext.util.JSON
							.decode(response.responseText);
					tree.getRootNode().appendChild(Ext.util.JSON
							.decode(responseTextVale));
				}
			});

}

Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8';
Ext.QuickTips.init();

var root = new Ext.tree.AsyncTreeNode({
			expanded : true
		});

var tree = new Ext.tree.TreePanel({
			loader : new Ext.tree.TreeLoader(),
			id : 'orgTree',

			rootVisible : false,
			region : 'west',
			split : true,
			border : true,
			collapsible : false,
			width : 160,
			minSize : 80,
			maxSize : 200,
			animate : true,
			autoScroll : true,
			enableDD : true,
			containerScroll : true

		});

getMenu(tree);

tree.setRootNode(root);

tree.on("click", function(node) {

			flow_orgid.setValue(node.id);

			positionstore.on('beforeload', function() {
						positionstore.baseParams = {
							'filter.flow_orgid' : Ext.getCmp('flow_orgid')
									.getValue(),
							'filter.roleName' : ''
						}
					});
			positionstore.reload();
		});

// 使用前请修改store & column 对应字段
var flow_orgid = new Ext.form.Hidden({
			name : 'flowRole.flow_orgid',
			id : 'flow_orgid',
			allowBlank : false,
			triggerAction : 'all'
		});
Ext.onReady(function() {
	// 查询角色start------------------------------------------------
	positionstore = new Ext.data.Store({
				url : 'workflowRoleAction.action?actionType=0',
				reader : new Ext.data.JsonReader({
							root : 'result.result',
							id : 'flow_roleid',
							totalProperty : 'result.totalCount'
						}, [{
									name : 'flow_rolecode',
									mapping : 'flow_rolecode'

								}, {
									name : 'flow_roleid',
									mapping : 'flow_roleid'
								}, {
									name : 'flow_rolename',
									mapping : 'flow_rolename'
								}, {
									name : 'flow_rolememo',
									mapping : 'flow_rolememo'
								}, {
									name : 'flow_orgid',
									mapping : 'flow_orgid'
								}, {
									name : 'flow_orgname',
									mapping : 'flow_orgname'
								}])
			});
	positionstore.baseParams = {
		'filter.flow_orgid' : '',
		'filter.roleName' : ''
	}
	positionstore.load();
	var positionsm = new Ext.grid.CheckboxSelectionModel();
	var positioncm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			positionsm, {
				header : '显示名称',
				width : 90,
				dataIndex : 'flow_rolename'
			}, {
				header : '角色名称',
				width : 90,
				dataIndex : 'flow_rolecode'
			}, {
				header : '所属组织',
				width : 90,
				// hidden:true,
				dataIndex : 'flow_orgname'
			}, {
				header : '对应帐号',
				dataIndex : 'flow_roleid',
				renderer : function(v, p, record) {
					return "<input type='button' value='查看'  onClick=\"showpositionWin('"
							+ v
							+ "','"
							+ record.data.flow_rolename
							+ "','"
							+ record.data.flow_orgid + "')\">";
				}
			}]);

	var positionGrid = new Ext.grid.GridPanel({
		title : '角色管理',
		region : 'center',
		trackMouseOver : true,
		stripeRows : true,
		autoScroll : true,
		hideCollapseTool : true,
		// selModel:new
		// Ext.grid.RowSelectionModel({singleSelect:false}),
		collapsible : true,
		titleCollapse : true,
		bufferResize : true,
		loadMask : true,

		height : document.body.clientHeight,
		id : 'positionGrid',
		tbar : ['角色名称', {
					xtype : 'textfield',
					id : 'flow_name'
				}, {
					xtype : "tbseparator"
				}, {
					xtype : 'button',
					text : '查询',
					handler : function() {
						positionstore.on('beforeload', function() {
									positionstore.baseParams = {
										'filter.roleName' : Ext
												.getCmp('flow_name').getValue()
									}
								});
						positionstore.reload();
					}
				}, {
					xtype : "tbseparator"
				}, {
					xtype : "tbbutton",
					text : "新增角色",
					handler : function() {
						positionWin.show();
					}
				}, {
					xtype : "tbseparator"
				}, {
					xtype : "tbbutton",
					text : "删除角色",
					handler : function() {
						var length = positionGrid.getSelectionModel()
								.getSelections().length;
						if (length > 0) {
							Ext.MessageBox.show({
								title : '删除提示',
								msg : '数据即将被删除',
								buttons : Ext.MessageBox.OKCANCEL,
								fn : function(button) {
									if (button == "ok") {
										var rs = positionGrid
												.getSelectionModel()
												.getSelections();
										var deleteAccounts = [];
										for (var i = 0; i < length; i++) {
											deleteAccounts
													.push(rs[i].data.flow_roleid);
											positionstore.remove(positionstore
													.getById(rs[i].id));
										};
										Ext.Ajax.request({
											url : 'workflowRoleAction.action?actionType=2',
											success : function() {
												positionstore.reload();
												Ext.Msg.show({
															title : '提示',
															msg : '删除成功',
															minWidth : 200,
															modal : true,
															icon : Ext.Msg.INFO,
															buttons : Ext.Msg.OK
														});
											},
											failure : function() {
												Ext.Msg.show({
															title : '提示',
															msg : '数据处理失败',
															minWidth : 200,
															modal : true,
															icon : Ext.Msg.INFO,
															buttons : Ext.Msg.OK
														});
											},
											params : {
												'filter.ids' : deleteAccounts
														.join(',')
											}
										});
									}
									if (button == "cancel") {
										return;
									}
								}
							});

						} else {
							Ext.Msg.show({
										title : '提示',
										msg : '请选择要删除的数据',
										minWidth : 200,
										modal : true,
										icon : Ext.Msg.INFO,
										buttons : Ext.Msg.OK
									});
						}
					}
				}

		],
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : positionstore,
					displayInfo : true,
					dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
					emptyMsg : '没有记录'
				}),
		sm : positionsm,
		cm : positioncm,
		store : positionstore,
		loadMask : true,
		loadMask : {
			msg : '正在加载数据...'
		}
	});
	// 查询角色end------------------------------------------------

	// 增加角色start------------------------------------------------
	// 组织store
	orgstore = new Ext.data.Store({
				url : 'workflowAccountAction.action?actionType=-1',
				reader : new Ext.data.JsonReader({
							root : 'orgComb',
							id : 'returnValue',
							totalProperty : ''
						}, [{
									name : 'flow_orgid',
									mapping : 'returnValue'
								}, {
									name : 'flow_orgname',
									mapping : 'displayText'
								}])
			});

	var newpositionForm = new Ext.form.FormPanel({
				// labelAlign: 'top',
				frame : true,
				region : 'north',
				id : 'newpositionForm',
				height : 200,
				bodyStyle : 'padding:5px 5px 0',
				// url : 'Tax!saveTax.action',
				items : [flow_orgid, {
							layout : 'column',

							items : [{
										columnWidth : 1,
										layout : 'form',
										buttonAlign : 'left',
										items : [{
													xtype : 'textfield',
													fieldLabel : '显示名称',
													name : 'flowRole.flow_rolename',
													id : 'flow_rolename',
													anchor : '80%',
													allowBlank : false,
													// blankText : '角色名称不能为空',
													emptyText : ''
												}, {
													xtype : 'textfield',
													fieldLabel : '角色名称',
													name : 'flowRole.flow_rolecode',
													id : 'flow_rolecode',
													anchor : '80%',
													allowBlank : false,
													// blankText : '角色名称不能为空',
													emptyText : ''
												}, {
													xtype : 'textarea',
													fieldLabel : '角色说明',
													name : 'flowRole.flow_rolememo',
													id : 'flow_rolememo',
													anchor : '80%',
													allowBlank : false,
													// blankText : '帐号不能为空',
													emptyText : ''
												}]
									}]
						}]
			});
	// 增加角色end------------------------------------------------

	var positionWin = new Ext.Window({
				closeAction : 'hide',
				closable : true,
				draggable : true,
				id : 'positionWin',
				region : 'center',
				title : '新增角色',
				width : 500,
				height : 300,
				modal : true,
				plain : true,
				items : [newpositionForm],
				buttons : [{
					text : '确定',
					id : 'save',
					handler : function() {
						if (null == flow_orgid.getValue()
								|| flow_orgid.getValue() == undefined
								|| flow_orgid.getValue() == "") {
							Ext.Msg.show({
										title : '提示',
										msg : '您尚未选择组织架构,无法提交',
										minWidth : 200,
										modal : true,
										icon : Ext.Msg.INFO,
										buttons : Ext.Msg.OK
									});
							return;
						};
						if (newpositionForm.getForm().isValid()) {
							newpositionForm.getForm().submit({
								url : 'workflowRoleAction.action?actionType=1',
								waitMsg : '正在提交数据...',
								success : function(request, result) {
									resutjson = Ext
											.decode(result.response.responseText);
									positionWin.hide();
									newpositionForm.getForm().reset();
									positionstore.reload();

									Ext.Msg.show({
												title : '提示',
												msg : '保存成功',
												minWidth : 200,
												modal : true,
												icon : Ext.Msg.INFO,
												buttons : Ext.Msg.OK
											});
								},
								failure : function() {
									positionWin.hide();
									newpositionForm.getForm().reset();
									positionstore.reload();
									Ext.Msg.show({
												title : '提示',
												msg : '保存失败',
												minWidth : 200,
												modal : true,
												icon : Ext.Msg.INFO,
												buttons : Ext.Msg.OK
											});
								}
							})
						}

					}
				}, {
					text : '取消',
					id : 'cancel',
					handler : function() {
						positionWin.hide();
					}
				}]
			});

	accountstore = new Ext.data.Store({
				url : 'workflowRoleAction!queryFlowUserByRole.action',
				reader : new Ext.data.JsonReader({
							root : 'roelUser.result',
							id : 'flow_userid',
							totalProperty : 'roelUser.totalCount'
						}, [{
									name : 'flow_usercode',
									mapping : 'flow_usercode'
								}, {
									name : 'flow_userid',
									mapping : 'flow_userid'
								}, {
									name : 'flow_username',
									mapping : 'flow_username'
								}, {
									name : 'flow_useremail',
									mapping : 'flow_useremail'
								}, {
									name : 'flow_orgid',
									mapping : 'flow_orgid'
								}, {
									name : 'flow_usermobile',
									mapping : 'flow_usermobile'
								}, {
									name : 'flow_orgName',
									mapping : 'flow_orgName'
								}])
			});
	// positionGrid.render(Ext.getBody());
	viewport = new Ext.Viewport({

				layout : 'border',
				items : [tree, positionGrid]
			});
});

// 查看对应帐号--------------------------------------

function showpositionWin(flow_roleid, role_name, flow_orgid) {
	accountstore.baseParams = {
		flow_roleid : flow_roleid
	};
	var accountsm = new Ext.grid.CheckboxSelectionModel();
	var accountcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			accountsm, {
				width : 90,
				hidden : true,
				dataIndex : 'flow_userid'
			}, {
				header : '帐号',
				width : 90,
				dataIndex : 'flow_usercode'
			}, {
				header : '显示名',
				dataIndex : 'flow_username'
			}, {
				header : '电子邮件',
				dataIndex : 'flow_useremail'
			}]);
	accountGrid = new Ext.grid.GridPanel({
		title : '账号管理',
		region : 'center',
		trackMouseOver : true,
		stripeRows : true,
		autoScroll : true,
		hideCollapseTool : true,
		bufferResize : true,
		loadMask : true,
		height : document.body.clientHeight - 50,
		id : 'accountGrid',
		tbar : ['帐号名称', {
					xtype : 'textfield',
					id : 'flow_name'
				}, {
					xtype : "tbseparator"
				}, {
					xtype : 'button',
					text : '查询',
					handler : function() {
						accountstore.baseParams = {
							flow_roleid : flow_roleid,
							flow_username : Ext.getCmp('flow_name').getValue()
						};
						accountstore.reload();
					}
				}, {
					xtype : "tbseparator"
				}, {
					xtype : "tbbutton",
					text : "新增帐号",
					handler : function() {
						addWin(flow_orgid, flow_roleid);
					}
				}, {
					xtype : "tbseparator"
				}, {
					xtype : "tbbutton",
					text : "删除帐号",
					handler : function() {
						var length = accountGrid.getSelectionModel()
								.getSelections().length;
						if (length > 0) {
							Ext.MessageBox.show({
								title : '删除提示',
								msg : '数据即将被删除',
								buttons : Ext.MessageBox.OKCANCEL,
								fn : function(button) {
									if (button == "ok") {
										var rs = accountGrid
												.getSelectionModel()
												.getSelections();
										var deleteAccounts = [];
										for (var i = 0; i < length; i++) {
											deleteAccounts
													.push(rs[i].data.flow_userid);
											accountstore.remove(accountstore
													.getById(rs[i].id));
										};
										Ext.Ajax.request({
											url : 'workflowRoleAction!deleteRoleUser.action',
											success : function() {

												accountstore.reload();
												Ext.Msg.alert('警告', '删除成功');
											},
											failure : function() {
												accountstore.reload();
												Ext.Msg.alert('警告', '数据处理失败');
											},
											params : {
												deleteAccounts : deleteAccounts
														.join(','),
												flow_roleid : flow_roleid
											}
										});
									}
									if (button == "cancel") {
										return;
									}
								}
							});
						} else {
							Ext.Msg.show({
										title : '提示',
										msg : '请选择要删除的数据',
										minWidth : 200,
										modal : true,
										icon : Ext.Msg.INFO,
										buttons : Ext.Msg.OK
									});
						}
					}
				}

		],
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : accountstore,
					displayInfo : true,
					dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
					emptyMsg : '没有记录'
				}),
		sm : accountsm,
		cm : accountcm,
		store : accountstore,
		loadMask : true,
		loadMask : {
			msg : '正在加载数据...'
		}
	});
	var showUserWin = new Ext.Window({
				width : 400,
				height : 400,
				modal : true,
				title : '账号查看',
				items : [accountGrid]
			});
	accountstore.reload();
	showUserWin.show();
}

// 新增对应帐号

function addWin(flow_orgid, flow_roleid) {
	var addAccountstore = new Ext.data.Store({
		url : 'workflowRoleAction!queryRoleUserNotInRoleUser.action?flow_orgid='
				+ flow_orgid,
		reader : new Ext.data.JsonReader({
					root : 'roelUser.result',
					id : 'flow_userid',
					totalProperty : 'roelUser.totalCount'
				}, [{
							name : 'flow_usercode',
							mapping : 'flow_usercode'
						}, {
							name : 'flow_userid',
							mapping : 'flow_userid'
						}, {
							name : 'flow_username',
							mapping : 'flow_username'
						}, {
							name : 'flow_useremail',
							mapping : 'flow_useremail'
						}, {
							name : 'flow_orgid',
							mapping : 'flow_orgid'
						}, {
							name : 'flow_usermobile',
							mapping : 'flow_usermobile'
						}, {
							name : 'flow_orgName',
							mapping : 'flow_orgName'
						}])
	});
	var addAccountsm = new Ext.grid.CheckboxSelectionModel();
	var addAccountcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			addAccountsm, {
				width : 90,
				hidden : true,
				dataIndex : 'flow_userid'
			}, {
				header : '帐号',
				width : 90,
				dataIndex : 'flow_usercode'
			}, {
				header : '显示名',
				dataIndex : 'flow_username'
			}, {
				header : '电子邮件',
				dataIndex : 'flow_useremail'
			}]);
	addWinAccountGrid = new Ext.grid.GridPanel({
		region : 'center',
		trackMouseOver : true,
		stripeRows : true,
		autoScroll : true,
		hideCollapseTool : true,
		bufferResize : true,
		loadMask : true,
		height : 250,
		// height : document.body.clientHeight,
		id : 'addWinAccountGrid',
		tbar : ['帐号名称', {
					xtype : 'textfield',
					id : 'flow_name_add'
				}, {
					xtype : "tbseparator"
				}, {
					xtype : 'button',
					text : '查询',
					handler : function() {
						addAccountstore.on('beforeload', function() {
									addAccountstore.baseParams = {
										flow_username : Ext
												.getCmp('flow_name_add')
												.getValue()
									}
								});
						addAccountstore.reload();
					}
				}, {
					xtype : "tbseparator"
				}, {
					xtype : "tbbutton",
					text : "确定",
					handler : function() {
						var length = addWinAccountGrid.getSelectionModel()
								.getSelections().length;
						if (length > 0) {
							Ext.MessageBox.show({
								title : '新增提示',
								msg : '即将新增数据',
								buttons : Ext.MessageBox.OKCANCEL,
								fn : function(button) {
									if (button == "ok") {
										var rs = addWinAccountGrid
												.getSelectionModel()
												.getSelections();
										var insertAccounts = [];
										for (var i = 0; i < length; i++) {
											insertAccounts
													.push(rs[i].data.flow_userid);
										};
										Ext.Ajax.request({
											url : 'workflowRoleAction!insertRoleUser.action',
											success : function() {
												addAccountstore.reload();
												addUserWin.hide();
												accountstore.reload();
												Ext.Msg.alert('警告', '新增成功');
											},
											failure : function() {
												addAccountstore.reload();
												Ext.Msg.alert('警告', '数据处理失败');
											},
											params : {
												accountIds : insertAccounts
														.join(','),
												flow_roleid : flow_roleid
											}
										});
									}
									if (button == "cancel") {
										return;
									}
								}
							});
						} else {
							Ext.Msg.show({
										title : '提示',
										msg : '请选择要新增的数据',
										minWidth : 200,
										modal : true,
										icon : Ext.Msg.INFO,
										buttons : Ext.Msg.OK
									});
						}
					}
				}

		],
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : addAccountstore,
					displayInfo : true,
					dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
					emptyMsg : '没有记录'
				}),
		sm : addAccountsm,
		cm : addAccountcm,
		store : addAccountstore,
		loadMask : true,
		loadMask : {
			msg : '正在加载数据...'
		}
	});
	var addUserWin = new Ext.Window({
				width : 400,
				modal : true,
				height : 300,
				title : '新增账号',
				items : [addWinAccountGrid]
			});
	addAccountstore.reload();
	addUserWin.show();

}